package com.miaosha.utils;

import com.google.gson.Gson;
import com.miaosha.entity.Goods;
import com.miaosha.entity.MiaoShaGoods;
import com.miaosha.service.GoodsService;
import com.miaosha.service.MiaoShaGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class RedisDataPreloader implements ApplicationRunner {

    @Autowired
    private MiaoShaGoodsService miaoShaGoodsService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        preloadGoodsData();
    }

    private void preloadGoodsData() {
        RedisUtil.setKey("123", "123");
        List<MiaoShaGoods> goodsList = miaoShaGoodsService.listAllNow();
        log.info("开始预热商品，共 {} 件商品", goodsList.size());
        goodsList.forEach(goods -> {
            log.info("预热商品: {}", goods);
            // 保存商品到redis中
            miaoShaGoodsService.add2Redis(goods);
        });
    }
}
